import java.util.Arrays;

public class ArrayList<E> {

    //元素的数量
    private int size;

    //存放元素的数组
    private E[] elements;

    //默认的长度
    private static final int  DEFAULT_CAPACITY = 3;

    public ArrayList(int capacity){
        //只要传入的大小小于10，默认为10
        capacity = (capacity < DEFAULT_CAPACITY)?DEFAULT_CAPACITY:capacity;
        this.elements = (E[]) new Object[capacity];
    }

    public ArrayList(){
        this(DEFAULT_CAPACITY);
    }


    //接口方法。。。。。。add,get等

    //获取元素的数量
    public int size(){ return size; }
    //判断是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //获取指定下标的元素
    public E get(int index){
        rangeCheck(index);
        return elements[index];
    }


    //设置index位置的元素,返回原来的元素
    public E set(int index,E element){
        rangeCheck(index);
        E old = elements[index];
        elements[index] =  element;
        return old;
    }

    //返回指定元素的索引，如果不存在，则返回-1
    public int indexOf(E element){
        if(element == null){
            for (int i = 0; i < size; i++) {
                if (elements[i] == null ) return i;
            }
        }else {
            for (int i = 0; i < size; i++) {
                if (element.equals(elements[i])) return i;
            }
        }
        return -1;
    }

    //清除全部
    public void clear(){
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }

    //添加元素,向数组后面添加
    public void add(E element){
        add(size,element);
    }

    //指定的下标插入元素
    public void add(int index,E element){
        rangeCheck(index);

        ensureCapacity(size+1);
        for (int i = size-1; i >= index; i--) {
            elements[i+1] = elements[i];
        }
        elements[index] = element;
        size++;
    }

    //删除指定的下标元素，返回被删除的元素
    public E remove(int index){
        rangeCheck(index);
        E old = elements[index];
        for (int i = index; i < size - 1; i++) {
            elements[i] = elements[i+1];
        }
        elements[--size] = null;
        return old;
    }

    public boolean contains(E o) {
        return indexOf(o) >= 0;
    }


    //判断是否下标越界
    private void rangeCheck(int index) {
        if (0 > index || index > size ){
            throw new IndexOutOfBoundsException("Index：" + index + ",下标越界。size为：" +size);
        }
    }

    //扩容
    private void ensureCapacity(int capacity) {

        int oldCapacity = elements.length;
        if(oldCapacity >= capacity)return;
        //新容量为旧容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println("老的容量："+oldCapacity+",新的容量："+newCapacity);
    }


    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder();
        builder.append("size=").append(size).append("[");
        for (int i = 0; i < size; i++) {
            if (i != 0){
                builder.append(", ");
            }
            builder.append(elements[i]);
        }
        builder.append("]");
        return builder.toString();
    }




}
